package com.jh.user.business.impl;

import cn.jh.common.exception.ServiceException;
import com.jh.user.business.BankBranchBusiness;
import com.jh.user.pojo.*;
import com.jh.user.repository.AreaRepository;
import com.jh.user.repository.BankBranchRepository;
import com.jh.user.repository.BankNumberRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BankBranchImpl implements BankBranchBusiness{

	private static final Logger LOG = LoggerFactory.getLogger(BankBranchImpl.class);

	@Autowired
	private BankBranchRepository bbr;

	@Autowired
	private EntityManager em;

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Autowired
	private BankNumberRepository bankNumberRepository;

	@Autowired
	private AreaRepository areaRepository;

	@Override
	public List<BankBranch> findAllBranch(String province, String city, String topName) {
		String sql ="select bank_name from t_bank_branch where (bank_name like '%"+ province + "%' or bank_name like'%" + city + "%') and top_name like '%"+ topName + "%'";
		return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(BankBranch.class));
	}

    @Override
    public List<String> findAllBranch2(String province, String city, String topName) {
        //String sql ="select bank_name from t_bank_branch where (bank_name like '%"+ province + "%' or bank_name like'%" + city + "%') and top_name like '%"+ topName + "%'";
        List list = bbr.querybranchinfo(province,city,topName);
        // TODO Auto-generated method stub
        return list;


        //return null;
    }

	@Override
	public List<BankBranch> queryBankList(String bankLocation, String province, String city) {
		String sql = "select * from t_bank_branch b where b.top_name like '%" + bankLocation + "%' and b.province like '%" +province + "%' and b.city like '%" + city + "%' or b.BANK_NAME like '%"+ bankLocation +"总行%'";
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(BankBranch.class));
	}


	@Override
	public List<Province> findProvince() {
		// TODO Auto-generated method stub
		return bbr.findAllProvince();
	}
	@Override
	public List<City> findCity(String provinceid) {
		// TODO Auto-generated method stub
		return bbr.findAllCity(provinceid);
	}

	@Override
	public List<City> findAllCity() {
		return bbr.findAllCity();
	}
	@Override
	public List<Area> findArea(String cityid) {
		// TODO Auto-generated method stub
		return bbr.findAllArea(cityid);
	}

	//根据银行名称获取银行编号
	@Override
	public BankBranch getBankCodeByBankName(String bankName) {
		return bbr.getBankCodeByBankName(bankName);
	}

	/**
	 * 处理银行支行信息
	 *
	 * @param bankNoList 联行号集合
	 * @param cityid       城市id
	 */
	@Override
	public void updateBankBranch(List<String> bankNoList, String cityid) throws ServiceException {
		City city = bbr.findByCityid(cityid);
		if(city == null) {
			throw new ServiceException("未找到该城市，请正确输入城市名称");
		}
		Province province = bbr.findByProvinceId(city.getProvinceid());
		if(province == null) {
			throw new ServiceException("未找到该城市对应的省份");
		}
		List<BankBranch> bankBranches = bbr.findBankBranch(bankNoList);
		List<BankBranch> bankBranchList = new ArrayList<>();

		for (BankBranch bankBranch : bankBranches) {
			if (bankBranch.isNeedProcess()) {
				bankBranch.setCity(city.getCity());
				bankBranch.setCityId(city.getCityid());
				bankBranch.setProvince(province.getProvince());
				bankBranch.setProvinceId(province.getProvinceid());
				bankBranchList.add(bankBranch);
			}
		}
		bbr.save(bankBranchList);
		LOG.info("=====bankBranchList size: " + bankBranchList.size());
	}

	@Override
	public List<Map<String, Object>> getBankNameList() {
		List<Map<String, Object>> list = new ArrayList<>();
		List<BankNumber> numbers = bankNumberRepository.findAll();
		for (BankNumber number : numbers) {
			long id = number.getId();
			String bankName = number.getBankName();
			String bankNumber = number.getBankNumber();

			Map<String, Object> map = new HashMap<>();
			map.put("id", id);
			map.put("bankName", bankName);
			map.put("bankNumber", bankNumber);
			list.add(map);
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> getBankBranchBy(String provinceId, String cityId, String topName) {
		List<Map<String, Object>> list = new ArrayList<>();
		List<BankBranch> branchList = bbr.findBankNameList(provinceId, cityId, topName);
		if (null != branchList && branchList.size() > 0) {
			for (BankBranch bankBranch : branchList) {
				int id = bankBranch.getId();
				String bankName = bankBranch.getBankName();
				String bankNo = bankBranch.getBankNo();

				Map<String, Object> map = new HashMap<>();
				map.put("id", id);
				map.put("bankName", bankName);
				map.put("bankNo", bankNo);
				list.add(map);
			}
			return list;
		}
		return null;
	}

	@Override
	public List<Area> queryAreaListByCityId(String cityId) throws ServiceException {
		List<Area> areaList = areaRepository.getByCityId(cityId);
		if (null == areaList || areaList.size() == 0) {
			LOG.info("未查询到县/区信息！cityId = {}", cityId);
			throw new ServiceException("未查询到县/区信息！");
		}
		return areaList;
	}

	@Override
	public List<String> findAllBranch3(String provinceId, String cityId, String topName) {
		return bbr.findAllBranch3(provinceId, cityId, topName);
	}
}
